package com.kotlin.book.api

import HttpApi
import com.google.gson.reflect.TypeToken
import com.kotlin.book.entry.Chapter

object ChapterApi {
    data class GetChapterListType(
        val code: Number,
        val message: String,
        val data: List<Chapter>
    )

    fun getChapter(
        param: Map<String, Any?>?,
        callback: (HttpApi.ApiResponse<Chapter>?) -> Unit
    ) {
        val type = object : TypeToken<HttpApi.ApiResponse<Chapter>>() {}.type
        HttpApi.get<Chapter>(
            "/chapter/getChapter",
            param ?: mapOf(),
            type
        ) { response ->
            response?.let { callback(it) } ?: callback(null)
        }
    }

    fun getChapterList(
        param: Map<String, Any?>?,
        callback: (HttpApi.ApiResponse<List<Chapter>>?) -> Unit
    ) {
        val type = object : TypeToken<HttpApi.ApiResponse<List<Chapter>>>() {}.type
        HttpApi.get<List<Chapter>>(
            "/chapter/getChapterList",
            param ?: mapOf(),
            type
        ) { response ->
            response?.let { callback(it) } ?: callback(null)
        }
    }

    fun getChapterDirectory(
        param: Map<String, Any?>?,
        callback: (HttpApi.ApiResponse<List<Chapter>>?) -> Unit
    ) {
        val type = object : TypeToken<HttpApi.ApiResponse<List<Chapter>>>() {}.type
        HttpApi.get<List<Chapter>>(
            "/chapter/getChapterDirectory",
            param ?: mapOf(),
            type
        ) { response ->
            response?.let { callback(it) } ?: callback(null)
        }
    }
}
